﻿@model DoeLibsMVC.Models.EditLoanableBasicModel

@*
    this page will be render a modal and doesn't contain a layout. it will be included on the title details page directly
*@

<div class="modal fade" id="addLoanableBasicModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">

            @using (Html.BeginForm("AddLoanable", "Title", FormMethod.Post, new { id = "addLoanableForm", enctype = "application/x-www-form-urlencoded" }))
            {

            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span class="closeButton" aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="myModalLabel">Add loanable</h4>
            </div>
            <div class="modal-body">
                <table class="formTable">
                    <tbody>
                        <tr>
                            <td>
                                @Html.LabelFor(model => model.Barcode)
                            </td>
                            <td>
                                <div>
                                    @Html.TextBoxFor(model => model.Barcode, new { @id = "titleBarcode", @class = "generalTxtBox", onBlur = "checkIdentifier()" })
                                    <input type="button" onclick="insertFreeBarcode()" value="generate free" class="generalBtn" />
                                    @Html.ValidationMessageFor(model => model.Barcode, null, new { id = "validationMessageBarcode" })
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                @Html.LabelFor(model => model.Category)
                            </td>
                            <td>
                                <div>
                                    @Html.TextBoxFor(model => model.Category, new { @id = "titleLocationCategory", @class = "generalTxtBox" })
                                    @Html.ValidationMessageFor(model => model.Category)
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                @Html.LabelFor(model => model.Location)
                            </td>
                            <td>
                                <div>
                                    @Html.TextBoxFor(model => model.Location, new { @id = "titleLocation", @class = "generalTxtBox" })
                                    @Html.ValidationMessageFor(model => model.Location)
                                </div>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <div class="modal-footer">
                <button type="submit" class="btn btn-primary" id="submitAddLoanableBasicForm">save</button>
            </div>
            }
        </div>
    </div>
</div>

<script type="text/javascript">
    function checkIdentifier() {
        $.ajax({
            dataType: "json",
            method: "GET",
            url: '/api/loanable?doeLibId=' + $("#titleBarcode").val()
        })
            .done(function (data) {
                $("#validationMessageBarcode").text("Identifier already in use").removeClass('field-validation-valid').addClass('field-validation-error');
            }).fail(function (data) {
                $("#validationMessageBarcode").text("");
            });
    }

    function insertFreeBarcode() {
        $.ajax({
            dataType: "json",
            method: "GET",
            url: '/api/loanable?generateFreeDoeLibsId',
            success: function (data) {
                $("#titleBarcode").val(data);
                $("#validationMessageBarcode").text("");
            }
        });
    }

    $("#addLoanableForm").submit(function () {
        console.log(getFormData($('#addLoanableForm')));

        $.ajax({
            url: '/api/title/@ViewBag.titleId',
            type: 'POST',
            beforeSend: function (xhr) {
                xhr.setRequestHeader("Authorization", getAuthorizationHeader('@User.UserId', ''));
            },
            data: getFormData($('#addLoanableForm')),
            dataType: "json"
        }).done(function (data) {
            console.log("success", data);

            setHttpMessage("Loanable successfully added", true);
            location.reload();
        }).fail(function (data) {
            console.log("fail", data);
            clearAllErrorFields('addLoanableForm');

            if (data.status == 400) {
                console.log("bad request");

                addErrorsToFields("model.", data.responseJSON);
            } else {
                console.log("status: " + data.status);
                //servererror, etc.
                setHttpMessageDynamic("Technical problems. Please try again", false);
            }
        });
        return false;
    });
</script>